package com.xshbbs.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xshbbs.entity.Block;
import com.xshbbs.entity.Partition;
import com.xshbbs.entity.Reply;
import com.xshbbs.entity.Reply2;
import com.xshbbs.entity.Topic;
import com.xshbbs.entity.User;
import com.xshbbs.service.BlockService;
import com.xshbbs.service.PartitionService;
import com.xshbbs.service.Reply2Service;
import com.xshbbs.service.ReplyService;
import com.xshbbs.service.TopicService;
import com.xshbbs.service.UserService;
import com.xshbbs.tool.TokenProccessor;

@Controller
public class PageSkippingController {
	
	@Autowired
	private UserService userService;
	@Autowired
	private PartitionService partitionService;
	@Autowired
	private BlockService blockService;
	@Autowired
	private TopicService topicService;
	@Autowired
	private ReplyService replyService;
	@Autowired
	private Reply2Service reply2Service;
	
	@RequestMapping("ps.login") 
	public String toLogin(HttpServletRequest request) {
		
		String token = TokenProccessor.getInstance().makeToken();
		System.out.println("token：" + token);
		request.getSession().setAttribute("token", token);
		return "/login.jsp";
		
	}
	
	@RequestMapping("forum")
	public String toIndex(HttpServletRequest request, HttpServletResponse response) {
		
		List<Partition> partitionList = this.partitionService.selectAllPartition();
		request.setAttribute("partitionList", partitionList);
		return "/forum.jsp";
		
	}
	
	@RequestMapping("block")
	public String toBlock(@RequestParam(required=true, defaultValue="1") int pageNum,
						 	@RequestParam(required=true, defaultValue="10") int pageSize,
						 	int bid, @RequestParam(required=true, defaultValue="lastreply") String orderBy,
			HttpServletRequest request, HttpServletResponse response) {
		
//		int bid = Integer.parseInt(request.getParameter("bid"));
		Block block = this.blockService.selectBlockById(bid);
		Partition partition = this.partitionService.selectPartitionById(block.getPid());
		
		PageHelper.startPage(pageNum, pageSize, orderBy + " desc,updatetime desc");
		List<Topic> topicList = this.topicService.selectAllTopicByBid(block.getId());
		PageInfo<Topic> page = new PageInfo<Topic>(topicList);
		
		request.setAttribute("orderBy", orderBy);
		request.setAttribute("block", block);
		request.setAttribute("partition", partition);
		request.setAttribute("topicList", topicList);
		request.setAttribute("page", page);
		return "/WEB-INF/block.jsp";
		
	}
	
	@RequestMapping("ps.createTopic")
	public String toCreateTopic(String operation, int bid, HttpServletRequest request, HttpServletResponse response) {
		
//		int bid = Integer.parseInt(request.getParameter("bid"));
		Block block = this.blockService.selectBlockById(bid);
		Partition partition = this.partitionService.selectPartitionById(block.getPid());
		
		if(operation.equals("updateTopic")) {
			int tid = Integer.parseInt(request.getParameter("tid"));
			Topic topic = this.topicService.selectTopicById(tid);
			request.setAttribute("topic", topic);
		} else if(operation.equals("createTopic")) {
			String token = TokenProccessor.getInstance().makeToken();
			request.getSession().setAttribute("token", token);
		}
		
		request.setAttribute("block", block);
		request.setAttribute("partition", partition);
		request.setAttribute("operation", operation);
		return "/WEB-INF/createtopic.jsp";
		
	}
	
	@RequestMapping("ps.topic")
	public String toTopic(@RequestParam(required=true, defaultValue="1") int pageNum,
						@RequestParam(required=true, defaultValue="10") int pageSize,
				int id,@RequestParam(required=true, defaultValue="0") int rid, String createtime, HttpServletRequest request, HttpServletResponse response) {
		
//		int id = Integer.parseInt(request.getParameter("id"));
		this.topicService.updateClicksById(id);
		
		Topic topic = this.topicService.selectTopicById(id);
		Block block = this.blockService.selectBlockById(topic.getBid());
		Partition partition = this.partitionService.selectPartitionById(block.getPid());
		
		if(createtime != null) {
			int count = this.replyService.selectReplyPositionByTid(topic.getId(), createtime);
			pageNum = (int) Math.ceil((double)(count+1)/pageSize);
			System.out.println(pageNum);
			request.setAttribute("rid", rid);
		} else {
			if(rid != 0) {
				Reply r = this.replyService.selectReplyById(rid);
				int count = this.replyService.selectReplyPositionByTid(topic.getId(), r.getCreatetime());
				pageNum = (int) Math.ceil((double)(count+1)/pageSize);
				request.setAttribute("rid", rid);
			}
		}
		
		PageHelper.startPage(pageNum, pageSize);
		List<Reply> replyList = this.replyService.selectAllRepliesByTid(id);
		PageInfo<Reply> page = new PageInfo<Reply>(replyList); 
		
		request.setAttribute("block", block);
		request.setAttribute("partition", partition);
		request.setAttribute("topic", topic);
		request.setAttribute("replyList", replyList);
		request.setAttribute("page", page);
		return "/WEB-INF/topic.jsp";
		
	}
	
	@RequestMapping("search")
	public String search(@RequestParam(required=true, defaultValue="1") int pageNum,
						@RequestParam(required=true, defaultValue="20") int pageSize,
			String search, HttpServletRequest request) {
		
		String[] searchSplit = search.split(" ");
		
		PageHelper.startPage(pageNum, pageSize);
		List<Topic> topicList = this.topicService.selectTopicBySearch(searchSplit);
		PageInfo<Topic> page = new PageInfo<Topic>(topicList);
		
		request.setAttribute("search", search);
		request.setAttribute("topicList", topicList);
		request.setAttribute("page", page);
		return "/WEB-INF/search.jsp";
		
	}
	
	@RequestMapping("userinfo")
	public String toUserInfo(HttpServletRequest request) {
		
		String selectedId = request.getParameter("selectedId");
		request.setAttribute("selectedId", selectedId);
		return "/WEB-INF/userinfo.jsp";
		
	}
	
	@RequestMapping("user")
	public String toUser(@RequestParam(required=true, defaultValue="1") int pageNum,
							@RequestParam(required=true, defaultValue="1") int rPageNum,
							@RequestParam(required=true, defaultValue="10") int pageSize,
							@RequestParam(required=true, defaultValue="topic") String mytopic, 
							int id, String selected, HttpServletRequest request) {
		
		User user = this.userService.selectUserById(id);
		request.setAttribute("targerUser", user);
		
		PageHelper.startPage(pageNum, pageSize);
		List<Topic> topicList = this.topicService.selectAllTopicByUid(user.getId());
		PageInfo<Topic> tPage = new PageInfo<Topic>(topicList);
		request.setAttribute("topicList", topicList);
		request.setAttribute("tPage", tPage);
		
		PageHelper.startPage(rPageNum, pageSize);
		List<Topic> replyList = this.topicService.selectAllReplyByUid(id);
		PageInfo<Topic> rPage = new PageInfo<Topic>(replyList);
		request.setAttribute("replyList", replyList);
		request.setAttribute("rPage", rPage);
		
		request.setAttribute("mytopic", mytopic);
		request.setAttribute("selected", selected);
		return "/WEB-INF/user.jsp";
		
	}
	
	@RequestMapping("notice")
	public String toNotice(@RequestParam(required=true, defaultValue="1") int pageNum,
							@RequestParam(required=true, defaultValue="1") int rPageNum,
							@RequestParam(required=true, defaultValue="10") int pageSize,
							@RequestParam(required=true, defaultValue="topic") String selected, 
							HttpServletRequest request) {
		
		User user = (User) request.getSession().getAttribute("user");
		
		PageHelper.startPage(pageNum, pageSize);
		List<Reply> topicList = this.replyService.selectAllReplyByUid(user.getId());
		PageInfo<Reply> tPage = new PageInfo<Reply>(topicList);
		request.setAttribute("topicList", topicList);
		request.setAttribute("tPage", tPage);
		
		PageHelper.startPage(rPageNum, pageSize);
		List<Reply2> replyList = this.reply2Service.selectAllReply2ByUid(user.getId());
		PageInfo<Reply2> rPage = new PageInfo<Reply2>(replyList);
		request.setAttribute("replyList", replyList);
		request.setAttribute("rPage", rPage);
		
		request.setAttribute("selected", selected);
		return "/WEB-INF/notice.jsp";
		
	}
	
}
